package com.fnst.caesar.servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;

import com.fnst.caesar.bean.*;
import com.fnst.caesar.util.SqlFactoryUtil;

/**
 * Servlet implementation class InputClass_Servlet
 */
public class InputClass_Servlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InputClass_Servlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doPost(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		String class_group = request.getParameter("class_group");
		String class_type = request.getParameter("class_type");
		String class_id = request.getParameter("class_id");
		String class_name = request.getParameter("class_name");
		String class_teacher = request.getParameter("class_teacher");
		String class_teachNo = request.getParameter("class_teachNo");
		String class_student = request.getParameter("class_student");
		String default_apply_point_tmp = request.getParameter("default_apply_point");
		int default_apply_point = 0;
		String limit_recommend_point_tmp = request.getParameter("limit_recommend_point");
		int limit_recommend_point = 0;
		String class_describe = request.getParameter("class_describe");
		
		if(default_apply_point_tmp == null || limit_recommend_point_tmp == null || class_id == null)
		{
			request.getRequestDispatcher("/WEB-INF/WEB/Input_Class.jsp").forward(request, response);
		}
		else
		{
			default_apply_point = Integer.parseInt(default_apply_point_tmp);
			limit_recommend_point = Integer.parseInt(limit_recommend_point_tmp);
			
			ClassBasic_Bean cbb = new ClassBasic_Bean();
			cbb.setClass_describe(class_describe+"");
			cbb.setClass_group(class_group);
			cbb.setClass_id(class_id);
			cbb.setClass_name(class_name);
			cbb.setClass_student(class_student);
			cbb.setClass_teacher(class_teacher);
			cbb.setClass_teachNo(class_teachNo);
			cbb.setClass_type(class_type);
			cbb.setDefault_apply_point(default_apply_point);
			cbb.setLimit_recommend_point(limit_recommend_point);
			
			SqlSessionFactory ssf = SqlFactoryUtil.getSqlSessionFactory(request);
			
			SqlSession sqlSession = ssf.openSession();
			int result = sqlSession.insert("com.fnst.caesar.mapper.insertClassBasic", cbb);
			sqlSession.commit();
			sqlSession.close();
			if(result != 1)
			{
				request.setAttribute("result", "sql_error");
				request.getRequestDispatcher("/WEB-INF/WEB/Input_Class.jsp").forward(request, response);
			}
			else
			{
				request.setAttribute("result", "insert_success");
				request.getRequestDispatcher("/WEB-INF/WEB/Input_Class.jsp").forward(request, response);
			}
		}
		
	}
	
}
